package com.webapp.service.impl;

import org.apache.commons.lang3.StringUtils;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.webapp.dao.LoginDao;
import com.webapp.model.User;
import com.webapp.service.LoginService;
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    LoginDao lDao;
    
    @Override
    public boolean checkLogin(String param) {
        final JSONObject obj=JSONObject.parseObject(param);
        final String username=StringUtils.isEmpty(obj.getString("username"))?null:obj.getString("username");
        final String password=StringUtils.isEmpty(obj.getString("password"))?null:obj.getString("password");
        User user = lDao.checkLogin(username, password);
        if(user==null){
            return false;
        }
        return true;
    }

    @Override
    public int updatePassword(String param) {
        final JSONObject obj=JSONObject.parseObject(param);
        final String username=StringUtils.isEmpty(obj.getString("username"))?null:obj.getString("username");
        final String password=StringUtils.isEmpty(obj.getString("password"))?null:obj.getString("password");
        User user = new User(username, password);
        User user2 = lDao.checkLogin(user.getUserName(), user.getPassword());
        if(user2==null){
            return -1;
        }
        int i = lDao.updatePassword(user);
        return i;
    }

}
